package com.ruoyi.forum.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.forum.domain.vo.PostCollectUpvote;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.forum.domain.ForumPostCollect;
import com.ruoyi.forum.service.IForumPostCollectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 贴子-收藏Controller
 * 
 * @author ruoyi
 * @date 2024-02-13
 */
@RestController
@RequestMapping("/forum/postCollect")
public class ForumPostCollectController extends BaseController
{
    @Autowired
    private IForumPostCollectService forumPostCollectService;

    /**
     * 查询贴子-收藏列表
     */
    @PreAuthorize("@ss.hasPermi('forum:postCollect:list')")
    @GetMapping("/list")
    public TableDataInfo list(ForumPostCollect forumPostCollect)
    {
        startPage();
        List<ForumPostCollect> list = forumPostCollectService.selectForumPostCollectList(forumPostCollect);
        return getDataTable(list);
    }

    /**
     * 导出贴子-收藏列表
     */
    @PreAuthorize("@ss.hasPermi('forum:postCollect:export')")
    @Log(title = "贴子-收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ForumPostCollect forumPostCollect)
    {
        List<ForumPostCollect> list = forumPostCollectService.selectForumPostCollectList(forumPostCollect);
        ExcelUtil<ForumPostCollect> util = new ExcelUtil<ForumPostCollect>(ForumPostCollect.class);
        util.exportExcel(response, list, "贴子-收藏数据");
    }

    /**
     * 获取贴子-收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('forum:postCollect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(forumPostCollectService.selectForumPostCollectById(id));
    }

    /**
     * 新增贴子-收藏
     */
    @PreAuthorize("@ss.hasPermi('forum:postCollect:add')")
    @Log(title = "贴子-收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ForumPostCollect forumPostCollect)
    {
        forumPostCollect.setCreateBy(getUsername());
        return toAjax(forumPostCollectService.insertForumPostCollect(forumPostCollect));
    }

    /**
     * 修改贴子-收藏
     */
    @PreAuthorize("@ss.hasPermi('forum:postCollect:edit')")
    @Log(title = "贴子-收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ForumPostCollect forumPostCollect)
    {
        forumPostCollect.setUpdateBy(getUsername());
        return toAjax(forumPostCollectService.updateForumPostCollect(forumPostCollect));
    }

    /**
     * 删除贴子-收藏
     */
    @PreAuthorize("@ss.hasPermi('forum:postCollect:remove')")
    @Log(title = "贴子-收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(forumPostCollectService.deleteForumPostCollectByIds(ids));
    }

    /**
     * 贴子-删除收藏(根据用户id和贴子ID)
     */
    @PreAuthorize("@ss.hasPermi('forum:postCollect:del')")
    @Log(title = "贴子-收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/del")
    public AjaxResult removeForPostUser(@RequestBody @Valid PostCollectUpvote postCollectUpvote)
    {
        return toAjax(forumPostCollectService.deleteForumPostCollectByUserPost(postCollectUpvote));
    }
}
